package core.controls;
import java.util.ArrayList;

import ibm.widgets.WButton;
import ibm.widgets.WTable;

import com.rational.test.ft.object.interfaces.TestObject;
import com.rational.test.util.regex.Regex;

import core.exceptions.ResultFailureException;
import core.testcases.TestObj;
/**
 * Description   : Functional Test Script
 * @author angel.tsvetkov
 */
public class Table extends Control<WTable>
{
	public Table(String propertyValue, String propertyName, String objClass,
			TestObject parent, String descriptiveName) {
		super(propertyValue, propertyName, objClass, parent, descriptiveName);
		// TODO Auto-generated constructor stub
	}
	
	public Table(TestObject object, String descriptiveName) {
		super(object, descriptiveName);
		this.obj = null;
	}

	/**
	 * Description : Get cell from table
	 * 
	 * @param tablePropValue
	 *            - property value which is used for table determination
	 * 
	 * @param tablePropName
	 *            - property name which is used for table determination
	 * 
	 * @param tableRow
	 *            - row of the table
	 * 
	 * @param tableColumn
	 *            - column of the table
	 * 
	 * @param parent
	 *            - area in which the table belongs.
	 */
	public String getCellValue(int tableColumn, int tableRow) throws ResultFailureException {
		String result = "";
		// Initialize variables
		WTable selectTable = null;

		try {
			// Allocate Table
			selectTable = this.get();

			// Get cell value
			result = WTable.getCellContents(selectTable, tableRow, tableColumn);

		} catch (Exception e) {
			throw new ResultFailureException(
					"Unexpected assertion on reading cell of " + this.getDescriptiveName());
		}
		finally
		{
			// Unregister object
			selectTable.unregister();
		}

		return result;
	}

	/**
	 * Description : Get rows count in the table
	 * 
	 * @param tablePropValue
	 *            - property value which is used for table determination
	 * 
	 * @param tablePropName
	 *            - property name which is used for table determination
	 * 
	 * @param parent
	 *            - area in which the table belongs.
	 */
	public int getRowsCount()
			throws ResultFailureException {
		int rows = 0;
		WTable selectTable = null;

		try {
			// Initialize Table
			selectTable = this.get();

			// Get row count
			rows = selectTable.getRowCount();

		} catch (Exception e) {
			throw new ResultFailureException(
					"Unexpected assertion on determine rows count of " + this.getDescriptiveName());
		}
		finally
		{
			// Unregister object
			selectTable.unregister();
		}

		return rows;
	}

	/**
	 * Description : Get columns count in the table
	 * 
	 * @param tablePropValue
	 *            - property value which is used for table determination
	 * 
	 * @param tablePropName
	 *            - property name which is used for table determination
	 * 
	 * @param parent
	 *            - area in which the table belongs.
	 */
	public int getColumnsCount()
			throws ResultFailureException {
		int columns = 0;
		WTable selectTable = null;

		try {
			// Initialize Table
			selectTable = this.get();

			// Get columns count
			columns = selectTable.getColumnCount();

		} catch (Exception e) {
			throw new ResultFailureException(
					"Unexpected assertion on determine columns count of " + this.getDescriptiveName());
		}
		finally
		{
			// Unregister object
			selectTable.unregister();
		}

		return columns;
	}

	/**
	 * Description : Convert HTML table to double string array
	 * 
	 * @param objectTable
	 *            - object representation of HTML table
	 */
	public static String[][] transformTable(WTable objectTable)
			throws ResultFailureException {
		String[][] arrayTable = null;
		WTable selectTable = null;
		int tableRows = 0;
		int tableColumns = 0;

		if (objectTable == null) {
			throw new ResultFailureException("No test object!");
		}

		try {
			// Initialize Table
			selectTable = objectTable;

			// Get Table dimensions
			tableRows = selectTable.getRowCount();
			tableColumns = selectTable.getColumnCount();

			// Initialize array
			arrayTable = new String[tableRows][tableColumns];

			for (int row = 0; row < tableRows; row++) {
				arrayTable[row] = WTable.getEntyreRow(selectTable, row);
			}

		} catch (Exception e) {
			throw new ResultFailureException(
					"Unexpected assertion on reading cell!");
		}
		finally
		{
			// Unregister object
			selectTable.unregister();
		}

		return arrayTable;
	}

	/**
	 * Description : Concert HTML table to double string array
	 * 
	 * @param tablePropValue
	 *            - property value which is used for table determination
	 * 
	 * @param tablePropName
	 *            - property name which is used for table determination
	 * 
	 * @param parent
	 *            - area in which the table belongs.
	 */
	public String[][] transformTable()
			throws ResultFailureException {
		String[][] arrayTable = null;
		WTable selectTable = null;
		int tableRows = 0;
		int tableColumns = 0;

		try {
			// Initialize Table
			selectTable = this.get();

			// Get Table dimensions
			tableRows = selectTable.getRowCount();
			tableColumns = selectTable.getColumnCount();

			// Initialize array
			arrayTable = new String[tableRows][tableColumns];

			for (int row = 0; row < tableRows; row++) {
				arrayTable[row] = WTable.getEntyreRow(selectTable, row);
			}

		} catch (Exception e) {
			throw new ResultFailureException(
					"Unexpected assertion on reading cell of " + this.getDescriptiveName());
		}
		finally
		{
			// Unregister object
			selectTable.unregister();
		}

		return arrayTable;
	}

	@Override
	public WTable get() {
		if(isCreatedFromTestObject())
		{
			this.obj = new WTable(testObject);
		}
		else
		{
			if(this.isCognosControl())
			{
				TestObject parent = this.getParent();
				this.obj = new WTable(parent.getChildren()[0]);
			}
			else
			{
				Regex toSearch = new Regex(this.PropValue(), Regex.MATCH_MULTILINE);
				this.obj = new WTable(toSearch,this.PropName(),this.getObjClass(),this.getParent());
			}
		}
		return this.obj;
	}

	@Override
	public boolean isEnabled() {
		return this.get().isEnabled();
	}

	@Override
	public boolean isExist() {
		return this.get().ensureObjectIsVisible();
	}
	
	public ArrayList<String[]> getEntireTableContent() 
	{
		return this.get().getEntireTableContent();
	}
	
	public String[] getEntireColumnContent(int col)
	{
		return this.get().getEntireColumnContent(col);
	}
	
	public String[] getEntireRowContent(int row)
	{
		return this.get().getEntireRowContent(row);
	}
	
}

